package ext.tianma.MaterialClassification;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ext.com.iba.IBAUtil;
import ext.tianma.bom.eco.BOMHelp;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.vc.VersionControlHelper;

public class PartClassificationChange implements RemoteAccess {

	/***
	 * 根据RFQ编号查询当前二级物料
	 * 
	 * @param RFQNumber
	 * @throws Exception 
	 */
	public static String searchPartByRFQ(String RFQNumber,String parentNumbrt) throws Exception {
		JSONObject ob = new JSONObject();
		JSONArray array = new JSONArray();
		List<WTPart> partLists = new ArrayList<WTPart>();
		WTPart part = null;
		try {
			if (parentNumbrt != null) {
				part = BOMHelp.getPart(parentNumbrt, "", "");
			} else if (RFQNumber != null) {
				// 根据RFQ编号获取PArt
//				part = CreatePartClassificationUtil.getPartByIBAValue("RFQNumber", RFQNumber);
				part = BOMHelp.getPart(RFQNumber, "", "");
			}
			System.out.println("RFQPart========>>"+part);
			if (part != null) {
				// 根据当前物料查询该物料相关BOM结构
				partLists = readBOM(part);
				System.out.println("partLists====>>"+partLists);
				// 将二级BOM封装成json传送到前端
				if (partLists.size() > 0) {
					for (int j = 0; j < partLists.size(); j++) {
						JSONObject obInner = new JSONObject();
						WTPart strPart = partLists.get(j);
						String nodeFileName = CreatePartClassificationUtil.getClassification(strPart);
						System.out.println("nodeFileName====>>" + nodeFileName);
						String RFQNumbers = (String) IBAUtil.getIBAValue(strPart, "partRFQNumber");
						System.out.println("RFQNumbers====>>"+RFQNumbers);
						String classIfaName = nodeFileName.substring(nodeFileName.lastIndexOf("/") + 1);
						obInner.put("material",classIfaName);
						obInner.put("partNumber", strPart.getNumber());
						obInner.put("RFQNumber",RFQNumbers);
						obInner.put("parentNumber", part.getNumber());
						array.put(obInner);
						System.out.println("array====>>"+array.toString());
					}
					ob.put("status", "success");
					ob.put("message", "查询成功！");
					ob.put("data", array);
					System.out.println("ob====>>"+ob.toString());

				}
			}else {
				ob.put("status", "false");
				ob.put("message", "无数据！");
				ob.put("data", array);
				System.out.println("ob====>>"+ob.toString());
			}
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ob.toString();
	}

	/***
	 * 获取bom结构 xin_gao1
	 * 
	 * @param part
	 * @param level
	 * @return
	 * @throws WTException
	 */
	private static List<WTPart> readBOM(WTPart part) throws WTException {
		List<WTPart> list =new ArrayList<WTPart>();
		QueryResult qr = PersistenceHelper.manager.navigate(part, WTPartUsageLink.USES_ROLE, WTPartUsageLink.class,
				false);
		while (qr.hasMoreElements()) {
			WTPartUsageLink link = (WTPartUsageLink) qr.nextElement();
			WTPartMaster childMaster = link.getUses();
			QueryResult iterationQr = VersionControlHelper.service.allVersionsOf(childMaster);
			WTPart childPart = (WTPart) iterationQr.nextElement();
			list.add(childPart);

			list.addAll(readBOM(childPart));

		}
		return list;
	}

}
